package com.lagou.rocket.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lagou.rocket.demo.constants.DelayLevel;
import com.lagou.rocket.demo.constants.TopicName;
import com.lagou.rocket.demo.enums.OrderStatus;
import com.lagou.rocket.demo.mapper.OrderMapper;
import com.lagou.rocket.demo.po.Order;
import com.lagou.rocket.demo.service.OrderService;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public boolean saveOrder(Order order) {
        final int inserted = orderMapper.insert(order);
        // 向延迟队列发送消息
        rocketMQTemplate.asyncSend(TopicName.CANCEL_ORDER_TOPIC, // 到哪个主题
                MessageBuilder.withPayload(order.getOrderNo()).build(), // 消息
                new SendCallback() { // 回调
                    @Override
                    public void onSuccess(SendResult sendResult) {
                        System.out.println("延迟消息发送成功");
                    }

                    @Override
                    public void onException(Throwable throwable) {
                        System.err.println(throwable.getLocalizedMessage());
                    }
                },
                5000, // 发送超时时间
                DelayLevel.LEVEL_3 // 延迟5秒
        );

        return inserted == 1;
    }

    @Override
    public boolean cancelOrder(Order order) {

        // 首先根据订单号查询该订单支付状态
        // 如果是未支付，则取消订单
        // 如果是已支付或者已取消，则不作任何操作

        final Order orderDB = orderMapper
                .selectOne(new QueryWrapper<Order>()
                        .eq("order_no", order.getOrderNo())
                );

        // 只要订单不是等待支付，就返回。无需处理
        if (orderDB.getStatus() != OrderStatus.CREATE.getCode()) {
            System.out.println("订单已结束");
            return false;
        }

        // 更新订单状态为取消
        final int updated = orderMapper.update(null,
                new UpdateWrapper<Order>()
                        .set("status", OrderStatus.CANCEL.getCode())
                        .set("update_time", new Date())
                        .eq("order_no", order.getOrderNo())
        );

        return updated == 1;
    }

    @Override
    public boolean updateOrder(Order order) {

        final int updated = orderMapper.update(null,
                new UpdateWrapper<Order>()
                        .set("status", OrderStatus.PAYED.getCode())
                        .set("update_time", new Date())
                        .eq("order_no", order.getOrderNo())
        );

        return updated == 1;
    }
}
